<template>
  <div v-if="visible" class="supplier-filter-overlay" @click="closeModal">
    <div class="supplier-filter-modal" @click.stop>
      <!-- 头部 -->
      <div class="modal-header">
        <h3 class="modal-title">选择供应商</h3>
        <button @click="closeModal" class="close-btn">×</button>
      </div>

      <!-- 供应商列表 -->
      <div class="modal-body">
        <div v-if="suppliers.length === 0" class="no-suppliers">
          <div class="empty-icon">🏢</div>
          <p>暂无供应商数据</p>
        </div>
        <div v-else class="supplier-list">
          <div 
            v-for="supplier in suppliers"
            :key="supplier"
            class="supplier-item"
            @click="selectSupplier(supplier)"
          >
            <div class="supplier-name">{{ supplier }}</div>
            <div class="supplier-count">{{ getSupplierProductCount(supplier) }} 个商品</div>
          </div>
        </div>
      </div>

      <!-- 底部按钮 -->
      <div class="modal-footer">
        <button @click="selectSupplier('')" class="btn btn-secondary">
          显示全部
        </button>
        <button @click="closeModal" class="btn btn-primary">
          取消
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits, computed } from 'vue';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  searchResults: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['close', 'select-supplier']);

// 获取去重的供应商列表
const suppliers = computed(() => {
  const supplierSet = new Set();
  props.searchResults.forEach(item => {
    if (item.supplier && item.supplier.trim()) {
      supplierSet.add(item.supplier.trim());
    }
  });
  return Array.from(supplierSet).sort();
});

// 获取每个供应商的商品数量
const getSupplierProductCount = (supplierName) => {
  return props.searchResults.filter(item => 
    item.supplier && item.supplier.trim() === supplierName
  ).length;
};

// 选择供应商
const selectSupplier = (supplierName) => {
  emit('select-supplier', supplierName);
  closeModal();
};

// 关闭弹窗
const closeModal = () => {
  emit('close');
};
</script>

<style scoped>
/* 弹窗覆盖层 */
.supplier-filter-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

/* 弹窗主体 */
.supplier-filter-modal {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

/* 头部 */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  color: #000000;
  margin: 0;
}

.close-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: #f5f5f5;
  border-radius: 50%;
  font-size: 20px;
  color: #666666;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #e0e0e0;
  color: #000000;
}

/* 主体内容 */
.modal-body {
  flex: 1;
  padding: 16px 20px;
  overflow-y: auto;
  max-height: 400px;
}

/* 无供应商状态 */
.no-suppliers {
  text-align: center;
  padding: 40px 20px;
  color: #666666;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.no-suppliers p {
  margin: 0;
  font-size: 16px;
}

/* 供应商列表 */
.supplier-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.supplier-item {
  padding: 16px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  background: #fff;
}

.supplier-item:hover {
  border-color: #f8b300;
  background: #fefbf2;
  transform: translateY(-1px);
}

.supplier-name {
  font-size: 16px;
  font-weight: 600;
  color: #000000;
  margin-bottom: 4px;
}

.supplier-count {
  font-size: 14px;
  color: #666666;
}

/* 底部按钮 */
.modal-footer {
  display: flex;
  gap: 12px;
  padding: 16px 20px 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.btn {
  flex: 1;
  padding: 12px 16px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary {
  background: #f5f5f5;
  color: #000000;
}

.btn-secondary:hover {
  background: #e0e0e0;
}

.btn-primary {
  background: #f8b300;
  color: #000000;
}

.btn-primary:hover {
  background: #e0a200;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .supplier-filter-overlay {
    padding: 12px;
  }
  
  .supplier-filter-modal {
    max-height: 90vh;
  }
  
  .modal-header {
    padding: 16px 16px 12px;
  }
  
  .modal-title {
    font-size: 16px;
  }
  
  .close-btn {
    width: 28px;
    height: 28px;
    font-size: 18px;
  }
  
  .modal-body {
    padding: 12px 16px;
    max-height: 60vh;
  }
  
  .supplier-item {
    padding: 12px;
  }
  
  .supplier-name {
    font-size: 15px;
  }
  
  .supplier-count {
    font-size: 13px;
  }
  
  .modal-footer {
    padding: 12px 16px 16px;
    flex-direction: column;
  }
  
  .btn {
    padding: 14px 16px;
  }
}

/* 触摸优化 */
.supplier-item,
.close-btn,
.btn {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* 滚动条样式 */
.modal-body::-webkit-scrollbar {
  width: 4px;
}

.modal-body::-webkit-scrollbar-track {
  background: transparent;
}

.modal-body::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

.modal-body::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}
</style>